package com.jy.controller;

import com.jy.common.UUIDUtils;
import com.jy.model.Account;
import com.jy.model.Product_release;
import com.jy.model.WithdrawalRecord;
import com.jy.service.ProductInvestmentService;
import com.jy.service.Product_releaseService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017/7/25 0025.
 */
@Controller
@RequestMapping("/productInvestment")
public class ProductInvestmentController {
    @Resource
    private ProductInvestmentService productInvestmentService;
    /**
     * 产品投资查询
     * @param start
     * @param length
     * @param
     * @return
     */
    @RequestMapping("/getProduct")
    public @ResponseBody
    List<Product_release> getProduct(Integer start, Integer length) {
        List<Product_release> list = productInvestmentService.getproduct(start,length);
        for (Product_release pr : list) {
            pr.setKymoney(pr.getMoney()-pr.getProduct_investment_amount());
        }
        return list;
    }
    @RequestMapping("/getProductInfo")
    public @ResponseBody Product_release getProductInfo(String id) {
        return productInvestmentService.tzInfo(id);
    }

    /*
		 * 产品投资
		 */
    @RequestMapping("/investment")
    public @ResponseBody
    Boolean investment(String product_investment_amount,String id) {

        int i = productInvestmentService.investment(product_investment_amount,id);
        if (i > 0) {

            return true;
        }else{
            return false;
        }

    }

    /*
       * 投资确认
       */
    @RequestMapping("/getcostInfo")
    public @ResponseBody
    List<Account> getcostInfo(Integer page, Integer rows, String userid,String member_category) {
        List<Account> list=null;
        if(member_category.equals("2")){
            list =productInvestmentService.getcostInfo(page, rows,null);
        }else {
            list =productInvestmentService.getcostInfo(page, rows,userid);
        }
       return  list;
    }

    /**
     * 投资确认单个查询
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getSinglecostInfo")
    public @ResponseBody
    Account getSinglecostInfo(String id) {
        return productInvestmentService.getSinglecostInfo(id);
    }

    /*
		 * 确认
		 */
    @RequestMapping("/confirm")
    public @ResponseBody
    Boolean confirm(String ysyl,String bh,String syls,String withdraw_time,String codes,String id) {
        Account account=new Account();
        account.setBh(bh);
        account.setQrsyl(Double.parseDouble(syls));
        account.setWithdraw_time(withdraw_time);
        account.setCodes(codes);
        account.setId(id);

			/* double syl=account.getQrsyl()/100;
			 account.setSyl(syl);*/
        //double ysyl=account.getSyl();
        double xsyl=account.getQrsyl();
        int i =0;
        if(Double.parseDouble(ysyl)==xsyl){
            account.setInvestment_status(1);
              i = productInvestmentService.confirm(account);

        }else{
            account.setInvestment_status(4);
            i = productInvestmentService.confirm(account);
        }
     if(i>0){
          return true;
     }else{
         return false;
     }


    }

    /*
		 * 提现确认
		 */
    @RequestMapping("cashfirm")
    public @ResponseBody
    boolean cashfirm(WithdrawalRecord wrs,String id ,String withdraw_time,String syls) throws Exception {

      Account account= productInvestmentService.getSinglecostInfo(id);

        int i =0;
        //提现申请 判断投资确认收益率与提现申请收益率是否相同
        if(account.getQrsyl()==Double.parseDouble(syls)){
            account.setApplication_status(2);
            wrs.setId(UUIDUtils.getUUID());
            wrs.setProduct_id(account.getProduct_name());//产品名
            wrs.setWithdraw_money(account.getInvestment_amount());//提现金额
            wrs.setWithdraw_sy(account.getInvestment_profit());//提现收益
            wrs.setUser_id(account.getUser_id());
            wrs.setType(account.getType());
            wrs.setSyl(account.getQrsyl());//投资原收益率
            wrs.setTjsyl(Double.parseDouble(syls));//提现现收益率
            wrs.setQrsyl(account.getQrsyl());////提现原收益率

            wrs.setInvestment_status(String.valueOf(account.getInvestment_status()));
            wrs.setApplication_status("2");
            wrs.setWithdraw_time(account.getApply_time()); //提现时间
            int truck = productInvestmentService.saveWithdraw(wrs);
           // account.setApply_time(apply_time);
            account.setSyl(Double.parseDouble(syls));
            account.setTjsyl(Double.parseDouble(syls));
            account.setWithdraw_time(withdraw_time);
            account.setApply_time(withdraw_time);
            i = productInvestmentService.cashfirm(account);//提现确认
        }else {
            account.setApplication_status(5);
            account.setSyl(Double.parseDouble(syls));
            account.setTjsyl(Double.parseDouble(syls));
            account.setWithdraw_time(withdraw_time);
            account.setApply_time(withdraw_time);
            i = productInvestmentService.cashfirm(account);//提现确认
        }
       if (i > 0) {

            return true;
        }else{
           return false;
       }

    }

    /**
     * 投资确认单个查询
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getaccrued")
    public @ResponseBody
    List<Account> getaccrued(Integer page, Integer rows,String id) {
        return productInvestmentService.getaccrued(page, rows, id);
    }

}
